import logging

import pytest

from resource.pom.keyboard import Keyboard


class Test:
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1、进入桌面，长按应用拖动合并到文件夹中')
        device.refresh_layout()
        badges = device.get_elements_by_type('Badge')
        x0, y0 = device.center_of_element(badges[0])
        x1, y1 = device.center_of_element(badges[1])
        device.drag(x0, y0, x1, y1)
        logging.info('预期结果1、应用合并到文件夹中无异常')
        device.refresh_layout()
        device.assert_text_exist('新建文件夹1')

        logging.info('步骤2、长按文件夹')
        new_folder = device.get_element_by_text('新建文件夹1')
        nx, ny = device.center_of_element(new_folder)
        device.long_click(nx, ny - 110)
        logging.info('预期结果2.长按文件夹显示重命名弹窗，输入新的名称')
        device.refresh_layout()
        device.assert_text_exist('重命名')

        logging.info('步骤3.点击重命名，输入新的名称')
        rename = device.get_element_by_text('重命名')
        device.click_element(rename)
        device.refresh_layout()
        add0 = device.get_element_by_text('添加')
        # 删除文件夹名称,点击文件夹命名框
        device.click(547, 209)
        device.click(360, 209)
        device.click(*self.keyboard.key_a)
        device.click(*self.keyboard.close)
        # 第一次退出编辑，第二次退出文件夹
        device.click(360, 640)
        device.click(360, 640)
        logging.info('预期结果3.桌面正常显示新的名称')
        device.refresh_layout()
        device.assert_text_exist('a')

        logging.info('步骤4.添加其他应用进入此文件夹')
        app0 = device.get_element_by_type('Badge')
        x0, y0 = device.center_of_element(app0)
        device.drag(x0, y0, nx, ny - 110)
        device.click(nx, ny - 110)
        device.refresh_layout()
        add1 = device.get_element_by_text('添加')
        logging.info('预期结果4.可以正常拖动其他应用进入此文件夹')
        assert device.center_of_element(add0) != device.center_of_element(add1), '应用未拖入文件夹'

        logging.info('步骤5.进入文件夹长按应用，点击移出文件夹')
        # 移2次则全部移出来了
        for i in range(2):
            device.long_click(220, 360)
            device.refresh_layout()
            move_out = device.get_element_by_text('移出文件夹')
            device.click_element(move_out)
            device.wait(0.5)

        logging.info('预期结果5.应用移出文件夹显示在桌面上')
        device.refresh_layout()
        device.assert_text_exist('a', expect=False)
